#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTime>
#include<QPen>
#include<QPainter>
#include<QPoint>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    resize(600,600);
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    timer->start(1000);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    static const QPoint hour[3]={
        QPoint(14,15),
        QPoint(-14,15),
        QPoint(0,-110)
    };
    static const QPoint minute[3]={
        QPoint(11,13),
        QPoint(-11,13),
        QPoint(0,-170)
    };
    static const QPoint second[3]={
        QPoint(7,8),
        QPoint(-7,8),
        QPoint(0,-210)
    };
    QFont font;
    int size=qMin(width(),height());
    QTime time=QTime::currentTime();
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    p.translate(width()/2,height()/2);
    p.scale(size/600.0,size/600.0);

    QBrush brush;
    brush.setColor(QColor(245,182,95));
    brush.setStyle(Qt::SolidPattern);

    QPen pen;
    pen.setWidth(18);
    pen.setColor(QColor(205,214,216));
    pen.setStyle(Qt::SolidLine);
    p.setPen(pen);

    p.drawEllipse(QPoint(0,0),280,280);
    pen.setColor(Qt::white);
    pen.setWidth(160);
    p.setPen(pen);
    p.drawEllipse(QPoint(0,0),160,160);
    //绘制时针
    p.setBrush(brush);
    p.setPen(Qt::NoPen);
    p.save();
    p.rotate(30.0*(time.hour()+time.minute()/60.0));
    p.drawConvexPolygon(hour,3);
    p.restore();
    //画刻度线(时)
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    pen.setColor(Qt::black);
    p.setPen(pen);
    for(int i=0;i<12;i++){
        p.drawLine(0,268,0,276);
        font.setPointSize(20); // 设置字体大小
        p.setFont(font);
        p.drawText(-5,-235,QString::number(i));
        p.rotate(30);
    }
    //画刻度线(分)
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    pen.setColor(Qt::black);
    p.setPen(pen);
    for(int i=0;i<60;i++){
        p.drawLine(0,276,0,288);

        p.rotate(6);
    }
    //绘制分针
    p.setPen(Qt::NoPen);
    p.setBrush(QColor(135, 206, 250));
    p.save();
    p.rotate(6*time.minute());
    p.drawConvexPolygon(minute,3);
    p.restore();
    //绘制秒针
    p.setPen(Qt::NoPen);
    p.setBrush(QColor(119,217,176));
    p.save();
    p.rotate(6*time.second());
    p.drawConvexPolygon(second,3);
    p.restore();

    p.setPen(Qt::black);
    if(time.hour()>=12){
        p.drawText(-6,-50,"PM");
    }else{
        p.drawText(-6,-50,"AM");
    }
}
